package com.su02.multi.chainrule;

import java.util.Map;

/**
 * f(x_0, x_1, ..., x_n) = x_i
 */
public class Variable extends Function {
    private final int id;

    public Variable(int id) {
        this.id = id;
        getBs().set(id);
    }

    @Override
    public Function doBackward(int id) {
        Function ans;
        if (getId() == id) {
            ans = new Constant(1);
        } else {
            ans = new Constant(0);
        }
        return ans;
    }

    @Override
    public double forward(Map<Integer, Double> vector) {
        return vector.get(getId());
    }

    public int getId() {
        return this.id;
    }
}
